package com.plan

import grails.converters.JSON

class WorkplanController {

	def springSecurityService
    def index = {
        redirect(action: "list", params: params)
    }

	def list = {
		def workplanInstanceList
		def user = springSecurityService.currentUser
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		workplanInstanceList = Workplan.findAllByCreater(user.username)
		
		[workplanInstanceList: workplanInstanceList, workplanInstanceTotal: Workplan.count()]
	}

    def create = {
				def user = springSecurityService.currentUser
        def workplanInstance = new Workplan()
        workplanInstance.properties = params
				workplanInstance.creater = user.username
        return [workplanInstance: workplanInstance]
    }

    def save = {
        def workplanInstance = new Workplan(params)
        if (workplanInstance.save(flush: true)) {
            flash.message = message(code: 'default.created.message', args: [message(code: 'workplan.label', default: 'Workplan'), workplanInstance.id])
            redirect(action: "edit", id: workplanInstance.id)
        }
        else {
            render(view: "create", model: [workplanInstance: workplanInstance])
        }
    }

    def show = {
        def workplanInstance = Workplan.get(params.id)
        if (!workplanInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'workplan.label', default: 'Workplan'), params.id])
            redirect(action: "list")
        }
        else {
            [workplanInstance: workplanInstance]
        }
    }

    def edit = {
        def workplanInstance = Workplan.get(params.id)
        if (!workplanInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'workplan.label', default: 'Workplan'), params.id])
            redirect(action: "list")
        }
        else {
            return [workplanInstance: workplanInstance]
        }
    }

    def update = {
        def workplanInstance = Workplan.get(params.id)
        if (workplanInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (workplanInstance.version > version) {
                    
                    workplanInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'workplan.label', default: 'Workplan')] as Object[], "Another user has updated this Workplan while you were editing")
                    render(view: "edit", model: [workplanInstance: workplanInstance])
                    return
                }
            }
            workplanInstance.properties = params
            if (!workplanInstance.hasErrors() && workplanInstance.save(flush: true)) {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'workplan.label', default: 'Workplan'), workplanInstance.id])
                redirect(action: "edit", id: workplanInstance.id)
            }
            else {
                render(view: "edit", model: [workplanInstance: workplanInstance])
            }
        }
        else {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'workplan.label', default: 'Workplan'), params.id])
            redirect(action: "list")
        }
    }

    def delete = {
        def workplanInstance = Workplan.get(params.id)
        if (workplanInstance) {
            try {
                workplanInstance.delete(flush: true)
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'workplan.label', default: 'Workplan'), params.id])
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'workplan.label', default: 'Workplan'), params.id])
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'workplan.label', default: 'Workplan'), params.id])
            redirect(action: "list")
        }
    }
}
